package com.lujianfei.waterpower.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.lujianfei.waterpower.R;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

public class CustomTitleBar extends ConstraintLayout {

    private TextView txt_title;
    TextView txt_right;
    TextView txt_left;
    ImageView img_right,img_left;
    private ConstraintLayout title_bar_root;

    private OnClickListener onLeftTxtClickListener;
    private OnClickListener onLeftImgClickListener;
    private OnClickListener onRightTxtClickListener;
    private OnClickListener onRightImgClickListener;
    public CustomTitleBar(Context context) {
        super(context);
        initView(context);
    }

    public CustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_titlebar,this,true);
        title_bar_root = findViewById(R.id.title_bar_root);
        txt_left = findViewById(R.id.txt_left);
        txt_title = findViewById(R.id.txt_title);
        txt_right = findViewById(R.id.txt_right);
        img_left = findViewById(R.id.img_left);
        img_right = findViewById(R.id.img_right);

        txt_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLeftTxtClickListener!=null){
                    onLeftTxtClickListener.onClick(view);
                }
            }
        });
        img_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLeftImgClickListener!=null){
                    onLeftImgClickListener.onClick(view);
                }
            }
        });
        txt_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRightTxtClickListener!=null){
                    onRightTxtClickListener.onClick(view);
                }
            }
        });
        img_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRightImgClickListener!=null){
                    onRightImgClickListener.onClick(view);
                }
            }
        });
    }

    public void setTitleMiddleText(String middleText) {
        txt_title.setText(middleText);
    }

    public void setTitleMiddleText(String middleText,boolean showMiddle) {
        setTitleMiddleText(middleText);
        if(showMiddle) {
            ConstraintSet set = new ConstraintSet();
            set.clone(title_bar_root);
            set.connect(R.id.txt_title, ConstraintSet.LEFT, R.id.txt_left, ConstraintSet.RIGHT);
            set.connect(R.id.txt_title, ConstraintSet.RIGHT, R.id.txt_right, ConstraintSet.LEFT);
            set.applyTo(title_bar_root);
        }
    }

    public void hideTitleLeft() {
        txt_left.setVisibility(View.GONE);
    }

    public void hideImgLeft() {
        img_left.setVisibility(View.GONE);
    }

    public void showTitleLeft() {
        txt_left.setVisibility(View.VISIBLE);
    }

    public void showImgLeft() {
        img_left.setVisibility(View.VISIBLE);
    }

    public void showTitleRight() {
        txt_right.setVisibility(View.VISIBLE);
    }

    public void showImgRight() {
        img_right.setVisibility(View.VISIBLE);
    }

    public void setTitleRightText(String rightText) {
        txt_right.setText(rightText);
    }

    public void setTitleRightImage(int resImg) {
        img_right.setImageResource(resImg);
    }

    public void hideTitleRightImage() {
        img_right.setVisibility(View.GONE);
    }

    public void showTitleRightImage() {
        img_right.setVisibility(View.VISIBLE);
    }

    public void showTitleRightText() {
        txt_right.setVisibility(View.VISIBLE);
    }

    public void hideTitleRightText() {
        txt_right.setVisibility(View.GONE);
    }

    public void setTitleLeftText(String leftText) {
        txt_left.setText(leftText);
    }

    public void showTitleLeftText() {
        txt_left.setVisibility(View.VISIBLE);
    }

    public void hideTitleLeftText() {
        txt_left.setVisibility(View.GONE);
    }

    public void setTitleLeftImage(int resImg) {
        img_left.setImageResource(resImg);
    }

    public void showTitleLeftImage() {
        img_left.setVisibility(View.VISIBLE);
    }

    public void hideTitleLeftImage() {
        img_left.setVisibility(View.GONE);
    }

    public void hideTitleRight() {
        txt_right.setVisibility(View.GONE);
    }

    public void setOnLeftTxtClickListener(OnClickListener onLeftTxtClickListener) {
        this.onLeftTxtClickListener = onLeftTxtClickListener;
    }

    public void setOnLeftImgClickListener(OnClickListener onLeftImgClickListener) {
        this.onLeftImgClickListener = onLeftImgClickListener;
    }

    public void setOnRightTxtClickListener(OnClickListener onRightTxtClickListener) {
        this.onRightTxtClickListener = onRightTxtClickListener;
    }

    public void setOnRightImgClickListener(OnClickListener onRightImgClickListener) {
        this.onRightImgClickListener = onRightImgClickListener;
    }

    public void setBackgroundColor(int color){
        title_bar_root.setBackgroundColor(color);
    }
}
